<?PHP

/**
 * General Public License as published by the Free Software Foundation
 * (see <http://www.gnu.org/licenses/ for more information).
 * @author Chris Reekie
 * @version 2/28/2011
 *
 * CIS 605
 * Donor DB Module
 */
/**
 * includes the file donor2.php once in this db control
 */
include_once('\dbConnect2.php');
include_once('\../model\Donor2.php');

/**
 * this function sets up the database for donor.
 */
function setup_dbDonor() {
    connect();
    $result = mysql_query("DROP TABLE IF EXISTS dbDonor");
    mysql_close();
    if ($result == false) {
        return false;
    }
    // Previous query was successful.  Proceed with creating the table.
    $query = "CREATE TABLE Donor(
                      `donorID` INT NOT NULL AUTO_INCREMENT ,
                      `street` VARCHAR(25) NULL ,
                      `city` VARCHAR(20) NULL ,
                      `stateName` VARCHAR(2) NULL ,
                      `zip` VARCHAR(10) NULL ,
                      `phone` VARCHAR(14) NULL ,
                      `fax` VARCHAR(14) NULL ,
                      `eMail` VARCHAR(30) NULL ,
                      PRIMARY KEY (`donorID`)
                            )ENGINE = InnoDB;";
    connect();
    $result = mysql_query($query);
    mysql_close();
    if ($result == false) {
        echo mysql_error();
        return false;
    }
    // query success
    return true;
}

/**
 * *this function gets the donorID field from its location
 */
function getdonorID($d) {
    connect();
    $result = mysql_query("SELECT donorID FROM donor WHERE donorID='$d'");
    $row = mysql_fetch_row($result);
    return $row[0];
    mysql_close();
}

/**
 * this function gets the street field from its location
 */
function getstreet($d) {
    connect();
    $result = mysql_query("SELECT street FROM donor WHERE donorID='$d'");
    $row = mysql_fetch_row($result);
    return $row[0];
    mysql_close();
}

/**
 *  this function gets the city field from its location
 */
function getcity($d) {
    connect();
    $result = mysql_query("SELECT city FROM donor WHERE donorID='$d'");
    $row = mysql_fetch_row($result);
    return $row[0];
    mysql_close();
}

/**
 *  this function gets the state field from its location
 */
function getstateName($d) {
    connect();
    $result = mysql_query("SELECT stateName FROM donor WHERE donorID='$d'");
    $row = mysql_fetch_row($result);
    return $row[0];
    mysql_close();
}

/**
 *  this function gets the zip field from its location
 */
function getzip($d) {
    connect();
    $result = mysql_query("SELECT zip FROM donor WHERE donorID='$d'");
    $row = mysql_fetch_row($result);
    mysql_close();
    return $row[0];
}

/**
 *  this function gets the phone field from its location
 */
function getphone($d) {
    connect();
    $result = mysql_query("SELECT phone FROM donor WHERE donorID='$d'");
    $row = mysql_fetch_row($result);
    return $row[0];
    mysql_close();
}

/**
 * 	 this function gets the fax field from its location
 */
function getfax($d) {
    connect();
    $result = mysql_query("SELECT fax FROM donor WHERE donorID='$d'");
    $row = mysql_fetch_row($result);
    return $row[0];
    mysql_close();
}

/**
 * this function gets the email field from its location
 */
function getemail($d) {
    connect();
    $result = mysql_query("SELECT email FROM donor WHERE donorID='$d'");
    $row = mysql_fetch_row($result);
    return $row[0];
    mysql_close();
}

/**
 * 	 this function adds all the fields from the donor table into their fields in the database.
 */
function adddonor($v) {
    return mysql_query("INSERT INTO donor VALUES
       ('" . $d->getdonorID() . "','" . $d->getdonoremail() . "','" . $d->getDonorID() . "','" . $d->getphone());
    mysql_close();
}

/**
 * this function deletes a donor from their location in the database
 */
function deletedonor($d) {
    connect();
    $i = $d->getdonorID();
    return mysql_query("DELETE FROM donor WHERE donorID='$d'");
    mysql_close();
}
function getAllOrganizations() {
    $query = "select O.*, D.*
                from organization as O
                inner join donor as D on D.donorID = O.donorID;";
    connect();
    $result = mysql_query($query);
	mysql_close();
    if ($result == null || mysql_num_rows($result) == 0) {
        return false;
    } else {
        return $result;
    }
}

/**
 * @return First&Last name from individual, and email from Donor
 * if none there, return false
 */
function getAllIndividuals() {
    $query = "select I.*, D.* from individual as I
              inner join donor as D on D.donorID = I.donorID;";
    connect();
    $result = mysql_query($query);
	mysql_close();
    if ($result == null || mysql_num_rows($result) == 0) {
        return false;
    } else {
        return $result;
    }
}
function searchIndvByLast($donorName) {
    $query = sprintf("select I.* , D.* from individual as I
                inner join donor as D on D.donorID = I.donorID
				WHERE I.lastName like '%s'",
				$donorName);
    connect();
    $result = mysql_query($query); 
	mysql_close();
    if ($result == null || mysql_num_rows($result) == 0) {
        return false;
    } else {
        return $result;
    }
}

function searchOrgByOrgName($donorName) {
    $query = sprintf("select O.*, D.* from organization as O
                inner join donor as D on D.donorID = O.donorID
				where O.organizationName like '%s'",
				$donorName);
    connect();
    $result = mysql_query($query);
	mysql_close();
    if ($result == null || mysql_num_rows($result) == 0) {
        return false;
    } else {
        return $result;
    }
}
function searchIndvByID($dID) {
    $query = sprintf("select I.* , D.* from individual as I
                inner join donor as D on D.donorID = I.donorID
				WHERE D.donorID = '%s'",
				$dID);
    connect();
    $result = mysql_query($query); 
	mysql_close();
    if ($result == null || mysql_num_rows($result) == 0) {
        return false;
    } else {
        return $result;
    }
}
function searchOrgByID($dID) {
    $query = sprintf("select O.* , D.* from Organization as O
                inner join donor as D on D.donorID = O.donorID
				WHERE D.donorID = '%s'",
				$dID);
    connect();
    $result = mysql_query($query); 
	mysql_close();
    if ($result == null || mysql_num_rows($result) == 0) {
        return false;
    } else {
        return $result;
    }
}
function updateDonorQuery($updateTable, $updateDonorID, $updateField, $updateValue) {
$query = sprintf("UPDATE '%s' 
				  SET '%s' = '%s'
				  WHERE donorID = '%s'",
				  $updateTable, $updateField, $updateValue, $updateDonorID);
    connect();
    $result = mysql_query($query); 
	mysql_close();
    if ($result == null || mysql_num_rows($result) == 0) {
        return false;
    } else {
        return $result;
    }
}
function updateIdnv($dID, $firstName, $lastName, $email, $phone, $fax, $street, $city, $state, $zip) {
$indvDonorArr=array($email, $phone, $fax, $street, $city, $state, $zip);
$indvArr=array($firstName, $lastName);
}



